/*
 * Copyright (C) 2025 Huawei Technologies Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.compose.ui.arkui.caniuse

import platform.init.canIUse

/**
 * CanIUse utility for checking HarmonyOS system capabilities.
 * Directly calls HarmonyOS C API for optimal performance.
 */
internal object CanIUseManager {
    private val capabilityCache = mutableMapOf<String, Boolean>()

    /**
     * Check if a system capability (SysCap) is available.
     * Directly calls HarmonyOS C API with no intermediate overhead.
     * Results are cached since system capabilities don't change at runtime.
     *
     * @param capability System capability name, e.g., "SystemCapability.ArkUI.ArkUI.Full"
     * @return true if available, false otherwise
     */
    fun checkCapability(capability: String): Boolean {
        // Check cache first
        capabilityCache[capability]?.let { return it }

        // Call HarmonyOS C API directly (via cinterop)
        val isAvailable = try {
            canIUse(capability)
        } catch (e: Exception) {
            androidx.compose.ui.graphics.kLog("CanIUseManager: checkCapability failed for $capability: ${e.message}")
            false
        }

        // Cache the result
        capabilityCache[capability] = isAvailable
        return isAvailable
    }
}
